package com.letoken.platform.pub.util;

import com.alibaba.fastjson.JSONObject;
import com.letoken.platform.pub.enums.LetokenApiResponseEnum;
import com.letoken.platform.pub.exception.ApiServiceException;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.springframework.stereotype.Component;

/**
 * @author letokenlabs
 * @date 2022/11/18$
 */
@Slf4j
@Component
public class CoingeckoUtil {

    public static String url = "https://api.coingecko.com/api/v3";

    public static JSONObject getTokenPrice(String ids, String vsCurrencies){
        return getTokenPrice(ids, vsCurrencies, null);
    }

    /**
     * 获取代币价格
     * @param ids
     * @param vsCurrencies
     * @param proxy
     * @return
     */
    public static JSONObject getTokenPrice(String ids, String vsCurrencies, HttpHost proxy) {
        String requestUrl = String.format("%s/simple/price?ids=%s&vs_currencies=%s", url, ids, vsCurrencies);
        String result = null;
        try {
            result = HttpClientUtil.sendGet(requestUrl.toString(), null, null, proxy);
        } catch (Exception e) {
            log.error("CoingeckoUtil getTokenPrice fail! " + e.getMessage());
            return null;
        }
        if (result != null) {
            return JSONObject.parseObject(result);
        }
        return null;
    }
}
